package com.quasar.hpatient.module.comm_compile_step2;

import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.DateDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.RadioDoubleDialog;
import com.quasar.hpatient.dialog.WarningDialog;
import com.quasar.hpatient.module.comm_compile.CompileActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseFragment;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.Aerainfo;
import lib.quasar.db.table.Hospital;

/**
 * description: 移植信息
 * created by kalu on 2018/4/9 14:57
 */
public class CompileStep2Fragment extends BaseFragment<CompileStep2Presenter> implements CompileStep2View {

    @Override
    public int initView() {
        return R.layout.fragment_comm_compile_step2;
    }

    @Override
    public void initDataLocal() {

        TextView box1 = getView().findViewById(R.id.compile_transplant_box1);
        if (null != box1) {
            final List<String> list = Arrays.asList("供体", "受体");
            box1.setOnClickListener(v -> {
                showRadio(box1, "选择供受体", list, 1);
            });
        }
        TextView box2 = getView().findViewById(R.id.compile_transplant_box2);
        if (null != box2) {
            final List<String> list = Arrays.asList("A型，Rh阳性", "B型，Rh阳性", "AB型，Rh阳性", "O型，Rh阳性", "A型，Rh阴性", "B型，Rh阴性", "AB型，Rh阴性", "O型，Rh阴性");
            box2.setOnClickListener(v -> showRadio(box2, "选择血型", list, 2));
        }
        TextView box3 = getView().findViewById(R.id.compile_transplant_box3);
        if (null != box3) {
            final List<String> list = Arrays.asList("肾移植", "肝移植", "肺移植", "心移植");
            box3.setOnClickListener(v -> showRadio(box3, "选择移植类型", list, 3));
        }
        TextView box4 = getView().findViewById(R.id.compile_transplant_box4);
        if (null != box4) {
            final List<String> list = Arrays.asList("亲属活体肾移植", "公民逝世器官捐献(DD)");
            box4.setOnClickListener(v -> showRadio(box4, "选择器官来源", list, 4));
        }
        TextView box5 = getView().findViewById(R.id.compile_transplant_box5);
        if (null != box5) {
            box5.setOnClickListener(v -> showDate(box5));
        }
        TextView box6 = getView().findViewById(R.id.compile_transplant_box6);
        if (null != box6) {
            box6.setOnClickListener(v -> showHospital(box6));
        }

        EditText box7 = getView().findViewById(R.id.compile_transplant_box7);
        if (null != box7) {
            box7.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    final String trim = s.toString().trim();
                    updataMesssage(16, trim);
                }
            });
        }
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void showRadio(TextView view, String title, List<String> list, int position) {

        final RadioDialog dialog = new RadioDialog(getActivity());
        dialog.show();
        dialog.setLoop(false);
        dialog.setTitle(title);
        dialog.setList(list);
        dialog.setOnMethodChangeListener(method -> {
            if (position == 2) {
                final WarningDialog warning = new WarningDialog(getActivity());
                warning.show();
                warning.setTitles("是否确认血型为阴性");
                warning.setButton("取消", "确定");
                warning.setOnDialogChangeListener((left, right) -> {
                    if (left) return;
                    updataMesssage(position + 9, method);
                    setText(view, method);
                });
            } else {
                updataMesssage(position + 9, method);
                setText(view, method);
            }
        });
    }

    @Override
    public void showDate(TextView view) {

        final DateDialog dialog = new DateDialog(getActivity());
        dialog.show();
        dialog.setTitles("选择手术时间");
        dialog.setOnDateChangeListener((year, month, day) -> {
            updataMesssage(14, year + "-" + month + "-" + day);
            setText(view, year + "-" + month + "-" + day);
        });
    }

    @Override
    public void showHospital(TextView view) {

        final List<String> cityList = new ArrayList<>();
        final List<String> hospitalList = new ArrayList<>();
        final RadioDoubleDialog dialog = new RadioDoubleDialog(getActivity());

        dialog.setOnDialogChangeListener((str1, str2, iswheel) -> {
            if (!iswheel) {
                updataMesssage(15, str2);
                setText(view, str2);
            } else if (!TextUtils.isEmpty(str1)) {
                String areaCode = DBManager.getInstance().getAreaCode(str1);
                final List<Hospital> list = DBManager.getInstance().getHospitalList(areaCode);
                hospitalList.clear();
                for (Hospital temp : list) {
                    hospitalList.add(temp.getName());
                }
                dialog.setList(null, hospitalList);
            }
        });
        dialog.show();

        final List<Aerainfo> list = DBManager.getInstance().getProvinceList();
        for (Aerainfo temp1 : list) {
            cityList.add(temp1.getAeraInfoName());
        }
        dialog.setList(cityList, null);
        dialog.setTitle("选择医院");

    }

    @Override
    public void updataMesssage(int position, String str) {

        final FragmentActivity activity = getActivity();
        if (null == activity || !(activity instanceof CompileActivity)) return;

        CompileActivity activity1 = (CompileActivity) activity;
        activity1.updateMessage(position, str);
    }

    @Override
    public boolean isNull() {

        final TextView box1 = getView().findViewById(R.id.compile_transplant_box1);
        final TextView box2 = getView().findViewById(R.id.compile_transplant_box2);
        final TextView box3 = getView().findViewById(R.id.compile_transplant_box3);
        final TextView box4 = getView().findViewById(R.id.compile_transplant_box4);
        final TextView box5 = getView().findViewById(R.id.compile_transplant_box5);
        final TextView box6 = getView().findViewById(R.id.compile_transplant_box6);
        final TextView box7 = getView().findViewById(R.id.compile_transplant_box7);

        return TextUtils.isEmpty(box1.getText().toString()) ||
                TextUtils.isEmpty(box2.getText().toString()) ||
                TextUtils.isEmpty(box3.getText().toString()) ||
                TextUtils.isEmpty(box4.getText().toString()) ||
                TextUtils.isEmpty(box5.getText().toString()) ||
                TextUtils.isEmpty(box6.getText().toString()) ||
                TextUtils.isEmpty(box7.getText().toString());
    }
}